package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.SendVerify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author zengguohang
 */
@RestController
@RequestMapping("verify")
public class VerifyController {
    @Autowired
    private IUserService userService;

    /**
     * 获取手机登录验证码
     *
     * @return
     */
    @PostMapping
    public JsonResult phoneSign(String phoneNumber, HttpSession session) {
        JsonResult result = new JsonResult();


        /* 这里验证手机号是否已经绑定账号 */
        boolean bind = userService.validataBindPhoneNumber(phoneNumber);

        try {
            if (!bind) {
                throw new RuntimeException("该手机没有绑定账号,请使用账号登录");
            }
            // 生成验证码
            String verify = (int) (Math.random() * 8999) + 1000+"";
            session.setAttribute("verify", verify);
            session.setAttribute("phoneNumber", phoneNumber);


            // 第三方短信通信接口参数设置
            if (!SendVerify.sendLoginVerify(phoneNumber, verify)) {
                result.mark("发送短信失败");

            }
            System.out.println(verify);


        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }


        return result;

    }
}